<?php

namespace C\P;

use C\P\Http;
use C\L\Logs;

const EVENT_CODE = [
    'activate' => 'ACTIVATE_APP',
    'register' => 'REGISTER',
];

class Tencent
{
    private $logger;
    private $accountId;
    private $accessToken;
    private $userActionSetId;

    /**
     * @param $accountId
     * @param $accessToken
     * @param $userActionSetId
     **/
    public function __construct($accountId, $accessToken, $userActionSetId)
    {
        $this->logger = (new Logs)->set('tencent_' . date('Ymd') . '.log');

        $this->accountId       = $accountId;
        $this->accessToken     = $accessToken;
        $this->userActionSetId = $userActionSetId;
    }

    /**
     * @param string $callbackUrl
     * @param string $event
     * @param array $data
     */
    public function report($callbackUrl, $event, $data)
    {
        $logInfo = [
            'callback_url' => $callbackUrl,
            'event'        => $event,
        ];
        $this->logger->notice(json_encode($logInfo));

        $eventCode = EVENT_CODE[$event] ?? -1;

        $url = $this->parseCallbackUrl($callbackUrl, $this->accessToken);

        $httpBody = $this->generateReportData([
          'account_id'         => $this->accountId,
          'user_action_set_id' => $this->userActionSetId,
          'action_timestamp'   => time(),
          'action_type'        => $eventCode,
          'hash_imei'          => $data['muid'] ?? '',
          'oaid'               => $data['oaid'] ?? '',
          'click_id'           => $data['click_id'] ?? ''
        ]);

        $this->logger->notice(json_encode($httpBody));

        $rawResponse = Http::curlPost($url, $httpBody);

        $this->logger->notice(json_encode($rawResponse));

        return true;
    }

    /**
     * @param array $params
     * @return string
     */
    private function generateReportData($params)
    {
        $data = [
            'account_id'         => $params['account_id'],
            'user_action_set_id' => $params['user_action_set_id'],
            'actions' => [
                [
                    'action_timestamp' => $params['action_timestamp'],
                    'action_type'      => $params['action_type'],
                    'user_id' => [
                        'hash_imei' => $params['hash_imei'],
                        'oaid'      => $params['oaid'],
                    ],
                    'trace' => [
                        'click_id' => $params['click_id'],
                    ],
                ],
            ],
        ];

        return json_encode($data);
    }

    /**
     * @param string $callbackUrl
     * @param string $accessToken
     * @return string
     */
    private function parseCallbackUrl($callbackUrl, $accessToken)
    {
        $url = urldecode($callbackUrl);
        $url = str_replace('<ACCESS_TOKEN>', $accessToken, $url);
        $url = str_replace('<TIMESTAMP>', time(), $url);
        $url = str_replace('<NONCE>', time(), $url);
        return $url;
    }
}
